// pages/MaintainScan/MaintainScan.js
const httpRequest = require('../../utils/http_utils.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '搜索',
    PageSize: 10, //每页显示条数
    CurrentPage: 1, //当前页数
    EquipmentName: "", //任务名称（输入框的值）
    TaskType: 1, //任务类型
    maintainList: [], //当天任务集合
    showIs: false, //图片显示
    noMore: false, //显示提示
    totalNum: 0, //数据条数
    scrollHeight: 0, //滚动区域高度
  },

  //监听输入框
  searchInput: function(e) {
    if (e.detail.value == null || e.detail.value == undefined || e.detail.value == ""){
      this.setData({
        maintainList: []
      })
      return
    }
    this.setData({
      EquipmentName: e.detail.value,
      maintainList:[]
    })
    this.initData();
  },

  //返回
  fanhui: function() {
    wx.navigateBack({
      delta: 1
    })
  },

  //设备跳转
  Device: function (e) {
    wx.navigateTo({
      url: '../Device/Device?ClassifyGuid=' + e.currentTarget.dataset.classifyguid + '&ClassifyName=' + e.currentTarget.dataset.classifyname
    })
  },

  //初始化数据
  initData: function() {
    var that = this;
    var param = {
      ClassName: that.data.EquipmentName
    }
    httpRequest.PostValidateRequest(httpRequest.apiInfo.GetMaintenanceScreenApi, param,
      function(data) {
        console.log("-------------搜索当天维保任务------------" + JSON.stringify(data))
        var listArray = that.data.maintainList;
        if (data.pageData.length == 0 || data.pageData == null || data.pageData == undefined) {
          that.setData({
            showIs: true
          })
        } else {
          for (var i = 0; i < data.pageData.length; i++) {
            listArray.push(data.pageData[i])
          }
          that.setData({
            maintainList: listArray,
            totalNum: data.pageData.length,
            noMore: false,
            showIs: false
          })
        }
      },
      function(err) {})
  },

  //上拉加载
  scrollToLower: function() {
    var that = this
    var pages = Math.round((that.data.totalNum / 10) * 100) / 100
    if (that.data.CurrentPage > pages) {
      that.setData({
        noMore: true
      })
      setTimeout(() => {
        that.setData({
          noMore: false
        })
      }, 2000)
    } else {
      that.setData({
        noMore: false,
        downloading: true
      })
      setTimeout(() => {
        that.setData({
          downloading: false
        })
      }, 3000)
      that.setData({
        CurrentPage: that.data.CurrentPage + 1
      })
      that.initData();
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      scrollHeight: ((app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 80
    })
    console.log(this.data.scrollHeight+"-------------------------滚动区域高度")
    this.refreshView = this.selectComponent("#refreshView")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  //触摸开始
  handletouchstart: function (event) {
    this.refreshView.handletouchstart(event)
  },
  //触摸移动
  handletouchmove: function (event) {
    this.refreshView.handletouchmove(event)
  },
  //触摸结束
  handletouchend: function (event) {
    this.refreshView.handletouchend(event)
  },
  //触摸取消
  handletouchcancel: function (event) {
    this.refreshView.handletouchcancel(event)
  },
  //页面滚动
  onPageScroll: function (event) {
    this.refreshView.onPageScroll(event)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    setTimeout(() => {
      this.setData({
        maintainList:[]
      })
      this.initData();
      this.refreshView.stopPullRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})